import request from '@/plugins/request'
import { IResponseData } from '@/types/request-base'
import { getStore, setStore } from '@/utils/store'
const SETTINGS = require('@/config/settings')

interface IDictData {
  dictTypeCode: string
  appCode?: string
  domainCode?: string
}

/**
 * 根据字典类型查询字典
 * @param {string} dictTypeCode 数据字典编码
 * @param {string} appCode 应用编码
 * @param {string} domainCode 业务域编码
 * @returns {Array} 数据字典值列表
 * */
export const getDictList = async (dictTypeCode: string) => {
  const data = await getDictByType({ dictTypeCode })
  return data
}

/**
 * 根据字典类型查询字典
 * @param {string} dictTypeCode 数据字典编码
 * @param {string} appCode 应用编码
 * @param {string} domainCode 业务域编码
 * @returns {Array} 数据字典值列表
 * */
export const getDictByType = async ({
  dictTypeCode,
  appCode = SETTINGS.appCode,
  domainCode = SETTINGS.domainCode
}: IDictData) => {
  const key = `${domainCode}_${appCode}_${dictTypeCode}`
  const data = getStore({ name: key })
  if (data && Array.isArray(data) && data.length > 0) {
    return data
  }
  let resData: any[] = []
  await request<IResponseData>({
    url: `/data-dict/app/v2/dictionary/${dictTypeCode}`,
    method: 'get',
    params: {
      appCode,
      domainCode
    }
  })
    .then((res) => {
      if (res && res.data) {
        resData = res.data.sort((a: any, b: any) => {
          return a.sort - b.sort
        })
      }
    })
    .catch(() => {})
  try {
    setStore({ name: key, content: resData, type: 'session' })
  } catch (error) {
    console.log(error)
  }
  return resData
}

/**
 * 根据字典父Id查询
 * @param {String} parentDictId 父Id
 * @returns {Array} 数据字典值列表
 * */
export const getDictChild = async (parentDictId: string) => {
  let resData: any[] = []
  await request<IResponseData>({
    url: `/data-dict/app/v2/dictionary/child`,
    method: 'get',
    params: { parentDictId }
  })
    .then((res) => {
      if (res && res.data) {
        resData = res.data.sort((a: any, b: any) => {
          return a.sort - b.sort
        })
      }
    })
    .catch(() => {})
  return resData
}

/**
 * 根据字典父类型与父编码查询子级数据字典
 * @param {String} parentDictTypeCode 父字段类型编码
 * @param {String} parentDictCode 父字典编码
 * @param {string} appCode 应用编码
 * @param {string} domainCode 业务域编码
 * @returns {Array} 数据字典值列表
 * */
export const getDictChildByCode = async ({
  parentDictTypeCode,
  parentDictCode,
  appCode = SETTINGS.appCode,
  domainCode = SETTINGS.domainCode
}: any) => {
  const data = new FormData()
  data.append('appCode', appCode)
  data.append('domainCode', domainCode)
  data.append('parentDictCode', parentDictCode)
  data.append('parentDictTypeCode', parentDictTypeCode)
  const res = await request<IResponseData>({
    url: `/data-dict/app/v2/dictionary/child`,
    method: 'post',
    data
  })
  return res && res.data
}

/**
 * 根据字典类型批量查询字典值列表
 * @param {Array} dictTypeCodes 数据字典编码
 * @returns {Array} 数据字典值列表
 * */
export const getDictByPatchType = async ({
  dictTypeCodes,
  appCode = SETTINGS.appCode,
  domainCode = SETTINGS.domainCode
}: any) => {
  const res = await request<IResponseData>({
    url: `/data-dict/app/v2/dictionary/patch/dictTypeCode`,
    method: 'post',
    params: {
      appCode,
      domainCode
    },
    data: dictTypeCodes
  })
  return res && res.data
}

// 获取省列表
export function getProvinces() {
  return request<IResponseData>({
    url: '/app/v1/mdmService/provinces',
    method: 'get',
    loading: false
  })
}

/**
 * 获取市、区县、街道列表
 * @param {String} parentId 父级ID
 */
export function getAreaList(parentId: any) {
  return request<IResponseData>({
    url: `/app/v1/mdmService/getByParentId/${parentId}`,
    method: 'get'
    // params: { parentId }
  })
}

/**
 * 获取移动应用页面权限按钮
 * @param {string} pagePerCode 页面权限编码
 *
 */
export function queryBtnList(pagePerCode: string) {
  return request({
    url: '/qmp/app/v1/button/current/buttons',
    method: 'get',
    params: {
      domainCode: SETTINGS.domainCode,
      appCode: 'CTM_APP' || SETTINGS.appCode,
      perCode: pagePerCode
    },
    loading: true // 是否显示加载
  })
}
